﻿using ContractLibrary;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace RILABomberman
{

    class Connection
    {
        private static Connection instance = null;
        private static IContractServer server;
        private static CallBackClient callback;

        private Connection()
	    {
            try
            {
                string serverIp = ConfigurationManager.AppSettings["ServerIP"];
                EndpointAddress serverAddress = new EndpointAddress("net.tcp://"+serverIp+":9080/DataService");
                callback = new CallBackClient();
                DuplexChannelFactory<IContractServer> cf = new DuplexChannelFactory<IContractServer>(callback, new NetTcpBinding(SecurityMode.None), serverAddress);
                server = cf.CreateChannel();
                
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
             
	    }

        public static Connection getInstance()
        {
            if (instance == null) instance = new Connection();
            return instance;
        }


        public static IContractServer getServer()
        {
            if(server == null)
            {
                string serverIp = ConfigurationManager.AppSettings["ServerIP"];
                EndpointAddress serverAddress = new EndpointAddress("net.tcp://" + serverIp + ":9080/DataService");
                callback = new CallBackClient();
                DuplexChannelFactory<IContractServer> cf = new DuplexChannelFactory<IContractServer>(callback, new NetTcpBinding(SecurityMode.None), serverAddress);
                server = cf.CreateChannel();
            }
            return server;
        }
       
    }
}
